package com.jill.jianghu.util.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.rest.RestActionSupport;

public class CommonActionSupport extends RestActionSupport implements ICommonActionSupport, SessionAware, ServletRequestAware, ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private String url;
	private String id;
	private Map<String, Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;

	
	/**
	 * 向客户端输出文字
	 * author: 刘涛
	 * datatime: Mar 18, 2010 3:42:23 PM
	 */
	public void printOut(final String str){
		try {
			getResponse().setContentType("text/html;charset=UTF-8");
			final PrintWriter out = getResponse().getWriter();
			out.print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * 获取request参数
	 * @param key
	 * @return
	 * author: 刘涛
	 * datatime: Mar 22, 2010 5:16:27 PM
	 */
	public String requestGetString(final String key){
		final String[] vals = getRequest().getParameterValues(key);
        if(null == vals || vals.length == 0){
        	return "";
        }
        StringBuffer sb =  new StringBuffer();
        for (int i = 0; i < vals.length; i++) {
        	if(i==vals.length-1){
        		sb.append(vals[i]);
        	}else{
        		sb.append(vals[i]).append(",");
        	}
		}
        return sb.toString();
	}


	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
	 */
	public void setSession(final Map<String, Object> session) {
		this.session = session;
	}


	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(final HttpServletRequest request) {
		this.request = request;
	}


	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
	 */
	public void setServletResponse(final HttpServletResponse response) {
		this.response = response;
	}
	

	public String getUrl() {
		return url;
	}


	public void setUrl(final String url) {
		this.url = url;
	}


	public String getId() {
		return id;
	}


	public void setId(final String id) {
		LOG.debug(getText("error.parse.long.id", id));
		this.id = id;
	}
	

	public Map<String, Object> getSession() {
		return session;
	}


	public HttpServletRequest getRequest() {
		return request;
	}


	public void setRequest(final HttpServletRequest request) {
		this.request = request;
	}


	public HttpServletResponse getResponse() {
		return response;
	}


	public void setResponse(final HttpServletResponse response) {
		this.response = response;
	}


	/* (non-Javadoc)
	 * @see com.jill.jianghu.util.action.ICommonActionSupport#create()
	 */
	public String create() {
		LOG.info("CommonActionSupport.create()");
		return SUCCESS;
	}


	/* (non-Javadoc)
	 * @see com.jill.jianghu.util.action.ICommonActionSupport#destroy()
	 */
	public String destroy() {
		LOG.info("CommonActionSupport.destroy()");
		return SUCCESS;
	}


	/* (non-Javadoc)
	 * @see com.jill.jianghu.util.action.ICommonActionSupport#edit()
	 */
	public String edit() {
		LOG.info("CommonActionSupport.edit()");
		return SUCCESS;
	}


	/* (non-Javadoc)
	 * @see com.jill.jianghu.util.action.ICommonActionSupport#editNew()
	 */
	public String editNew() {
		LOG.info("CommonActionSupport.editNew()");
		return SUCCESS;
	}


	/* (non-Javadoc)
	 * @see com.jill.jianghu.util.action.ICommonActionSupport#show()
	 */
	public String show() {
		LOG.info("CommonActionSupport.show()");
		return SUCCESS;
	}


	/* (non-Javadoc)
	 * @see com.jill.jianghu.util.action.ICommonActionSupport#update()
	 */
	public String update() {
		LOG.info("CommonActionSupport.update()");
		return SUCCESS;
	}
}
